package com.example.demo.config.shiro;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {

    public static final String SID = "__sid";

    public static void setSessionIdCookie(ServletRequest request, ServletResponse response, Cookie template, String sid) {
        HttpServletRequest rq = (HttpServletRequest)request;
        HttpServletResponse rs = (HttpServletResponse)response;
        Cookie cookie = new SimpleCookie(template);
        cookie.setPath("/");
        cookie.setValue(sid);
        cookie.saveTo(rq, rs);
        if (StringUtils.isEmpty(cookie.getName()))
            cookie.setName(SID);
    }

    public static Cookie getSessionIdCookie() {
        Cookie cookie = new SimpleCookie();
        cookie.setPath("/");
        cookie.setName(SID);

        return cookie;
    }

}
